package yl.hs.bmipfull.service.flow.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.flow.IFlowLogDao;
import yl.hs.bmipfull.pojo.flow.FlowLogEntity;
import yl.hs.bmipfull.service.flow.IFlowLogService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  流程日志表服务层接口实现<br/>
 * 创建时间: 2021年06月25日 09时20分14秒 <br/>
 *
 * @author 张凯
 */
@Service
public class FlowLogServiceImpl extends SystemBaseService implements IFlowLogService {

    @Autowired
    IFlowLogDao dao;

    @Override
    public List<FlowLogEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(FlowLogEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<FlowLogEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(FlowLogEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<FlowLogEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public FlowLogEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFlowLog(Map<String, Object> model, String sn) throws Exception {
        Integer result;
        FlowLogEntity entity;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new FlowLogEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(FlowLogEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入流程日志表信息", Tl.toJson(entity), FlowLogEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新流程日志表信息", Tl.toJson(entity), FlowLogEntity.class);
            }
        }

        return result;
    }

    @Override
    public List<FlowLogEntity> queryByFlowDataSn(String flowDataSn) {
        return dao.queryByFlowDataSn(flowDataSn);
    }

    @Override
    public Integer saveFlowLog(FlowLogEntity flowLogEntity) throws Exception {
        flowLogEntity.setSn(queryNewSN(FlowLogEntity.class));
        flowLogEntity.setPYFieldValue();
        return dao.insertNew(flowLogEntity);
    }

}